package com.jf.hb.ui.interfaces;

import com.jf.hb.HB;
import com.jf.hb.ui.JFHandler;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-08-19
 * Time:1:35
 */
public class Wish extends Interface {
    private boolean live;

    private ImageIcon bk;

    private JTextArea text;
    private String wishText;

    private Box hbox;

    private JButton saveButton;
    private JButton homeButton;
    private ImageIcon[] homeIcons;
    private int homeIndex;

    public Wish() {
        live = true;
        hbox = Box.createHorizontalBox();
        hbox.setSize(1280,720);
        bk = new ImageIcon(Objects.requireNonNull(getClass().getResource("/img/wish/bk.png")));
        wishText = HB.wishText;
        initText();
        initButton();
        add(hbox);
    }

    private void initText() {
        Box textBox = Box.createVerticalBox();
        textBox.setSize(300,720);
        text = new JTextArea();
        text.setSize(300,400);
        text.setText(wishText);
        text.setWrapStyleWord(true);
        text.setLineWrap(true);
        text.setOpaque(false);
        text.setFont(new Font("微软雅黑", Font.PLAIN, 26));

        hbox.add(Box.createHorizontalStrut(380));
        textBox.add(Box.createVerticalStrut(180));
        textBox.add(text);
        hbox.add(textBox);
    }

    private void initButton() {
        Box buttonBox = Box.createVerticalBox();
        buttonBox.setSize(86,720);

        saveButton = new JButton("保存");
        saveButton.addActionListener(e -> {
            HB.wishText = text.getText();
            HB.writeWishText();
        });

        homeButton = new JButton();
        homeIcons = new ImageIcon[12];
        for (int i = 0; i < 12; i++) {
            String path = "/img/blessing/button/button (" + (i + 1) + ").png";
            homeIcons[i] = new ImageIcon(Objects.requireNonNull(getClass().getResource(path)));
        }
        homeButton.setIcon(homeIcons[0]);
        homeButton.setOpaque(false);
        homeButton.setContentAreaFilled(false);
        homeButton.setBorderPainted(false);
        homeButton.setFocusPainted(false);
        homeButton.setBorder(null);
        homeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                HB.wishText = text.getText();
                HB.writeWishText();
                live = false;
                JFHandler.switchSignal = JFHandler.IF.WISH;
            }
        });

        hbox.add(Box.createHorizontalStrut(300));

        buttonBox.add(Box.createVerticalStrut(450));
        buttonBox.add(saveButton);
        buttonBox.add(Box.createVerticalStrut(20));
        buttonBox.add(homeButton);

        hbox.add(buttonBox);
        hbox.add(Box.createHorizontalStrut(214));
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(bk.getImage(), 0, 0, 1280, 720, null);
    }

    private void displayHomeButton() {
        homeIndex++;
        if (homeIndex > homeIcons.length - 1) {
            homeIndex = 0;
        }
        homeButton.setIcon(homeIcons[homeIndex]);
    }

    public void init() {
        wishText = HB.wishText;
        text.setText(wishText);
        live = true;
    }


    @Override
    public void run() {
        while(live) {
            try {
                displayHomeButton();
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
